﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Tongiao.Manager;
using Tongiao.Entity;

public partial class admin_News_NewsList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Pager1.ChangePage += new EventHandler(Pager1_ChangePage);
        if (!IsPostBack)
        {
            LoadCatagory();
            LoadData();
        }

    }

    void Pager1_ChangePage(object sender, EventArgs e)
    {
        LoadData();
    }

    protected void btnAddNews_Click(object sender, EventArgs e)
    {
        Response.Redirect("?control=NNewsAddEdit");
    }

    void LoadCatagory()
    {
        string catType = "NEWS";
        DataTable dtCat = Tg_CategoryManager.CreateInstant().getAllCategoryListByCatType(catType);
        if (dtCat.Rows.Count > 0)
        {
            ddlCategory.DataSource = dtCat;
            ddlCategory.DataTextField = "CatNameChild";
            ddlCategory.DataValueField = "IdChild";
            ddlCategory.DataGroupField = "CatName";

            ddlCategory.DataBind();

        }

    }

    void LoadData()
    {
    
          DataTable  dt = Tg_NewsInfosManager.CreateInstant().SearchNews((ddlCategory.SelectedValue.ToString() == "" ? Guid.Empty : new Guid(ddlCategory.SelectedValue.ToString())), txtTitle.Text.Trim());
        
        if (dt != null && dt.Rows.Count >= 0)
        {
            Pager1.DataSource = dt.DefaultView;
            Pager1.BindToControl = rep;
            rep.DataSource = dt;
            rep.DataBind();
        }


    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        LoadData();
    }

    public string GetEditUrl(object id)
    {

        return "?control=NNewsAddEdit&id=" + id;

    }

    protected void rep_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "del":
                Tg_NewsInfosManager.CreateInstant().DeleteById(new Guid(e.CommandArgument.ToString()));
                
                LoadData();
                break;
        }
    }
}
